package com.nchu.config;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginInterceptor implements HandlerInterceptor {

    /**
     * 目标方法执行前
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        Object loginUser  = request.getSession().getAttribute("currentUser");
        if (loginUser == null) {
            //未登录，返回登陆页
            request.setAttribute("loginFail", "您没有权限进行此操作，请先登陆！");
            request.getRequestDispatcher("/homepage.html").forward(request, response);
            return false;
        } else {
            //放行
            return true;
        }
    }

}
